/*
 * DetailyUkolu.java
 *
 * Created on 20.11.2011, 23:46:35
 */
package gui;

import database.DatabazovaChyba;
import gui.comboBoxRenderers.ProjektComboBoxRenderer;
import gui.comboBoxRenderers.StavComboBoxRenderer;
import gui.comboBoxRenderers.VlastnikComboBoxRenderer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import system.Launcher;
import ukoly.Komentar;
import ukoly.ProjektControl;
import ukoly.UkolControl;
import ukoly.UkolStatus;
import uzivatele.Uzivatel;
import uzivatele.UzivatelRank;

/**
 *
 * @author Jan Lukes, Pavel Lieberzeit
 */
public class DetailyUkolu extends javax.swing.JFrame {

    private UkolControl ukol;

    /** Creates new form DetailyUkolu 
     *  @param ukol Ukol, ktery se edituje nebo null (kdyz se vytvari novy)
     */
    public DetailyUkolu(UkolControl ukol) {
        this.ukol = ukol;
        initComponents();
        String nazevOkna = "Editace úkolu";
        if (ukol == null) {
            nazevOkna = "Vytvoření úkolu";
        }
        this.setTitle("Správce úkolů - " + nazevOkna
                + " - Přihlášen " + Launcher.getInstance().getUzivatel().getEmail()
                + " - " + UzivatelRank.getNameFromRankId(Launcher.getInstance().getUzivatel().getRank()));
//        Uzivatel uzivatel = Launcher.getInstance().getUzivatel();
//        String pravaUzivatele = UzivatelRank.getNameFromRankId(uzivatel.getRank());
//        String jmeno = uzivatel.getJmeno();
//        this.setTitle(jmeno + " (" + pravaUzivatele + ")");
        Launcher.getInstance().setOknoDoprostred(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Uzivatel uzivatel = Launcher.getInstance().getUzivatel(); //cache, trochu to zrychly celou inicializaci
        nazevLabel = new javax.swing.JLabel();
        popisLabel = new javax.swing.JLabel();
        nazevTextField = new javax.swing.JTextField();
        popisScrollPane = new javax.swing.JScrollPane();
        popisTextArea = new javax.swing.JTextArea();
        vlastnikLabel = new javax.swing.JLabel();
        vlastnikComboBox = new javax.swing.JComboBox();
        komentarLabel = new javax.swing.JLabel();
        komentarScrollPane = new javax.swing.JScrollPane();
        komentarEditorPane = new javax.swing.JEditorPane();
        dateCreateTextField = new javax.swing.JTextField();
        dateFinishTextField = new javax.swing.JTextField();
        dateCreateLabel = new javax.swing.JLabel();
        dateFinishLabel = new javax.swing.JLabel();
        dateDeadTextField = new javax.swing.JTextField();
        dateDeadLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        stavLabel = new javax.swing.JLabel();
        stavComboBox = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();
        projektLabel = new javax.swing.JLabel();
        projektComboBox = new javax.swing.JComboBox();
        casStravenLabel = new javax.swing.JLabel();
        casStravenTextField = new javax.swing.JTextField();
        komentarEntryScrollPane = new javax.swing.JScrollPane();
        komentarEntryTextArea = new javax.swing.JTextArea();
        komentarAddButton = new javax.swing.JButton();
        ukolMenuBar = new javax.swing.JMenuBar();
        ukolMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        nazevLabel.setText("Název:");

        popisLabel.setText("Popis:");

        if(this.ukol == null) {
            nazevTextField.setText("");
        } else {
            nazevTextField.setText(this.ukol.getModel().getJmeno());
        }
        nazevTextField.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);
        if(uzivatel.getRank() == 0)
        if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
        nazevTextField.setEnabled(false);

        popisTextArea.setColumns(20);
        popisTextArea.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        popisTextArea.setLineWrap(true);
        popisTextArea.setRows(5);
        popisTextArea.setWrapStyleWord(true);
        if(this.ukol == null) {
            popisTextArea.setText("");
        } else {
            popisTextArea.setText(this.ukol.getModel().getPopis());
        }

        if(uzivatel.getRank() == 0)
        if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
        popisTextArea.setEnabled(false);
        popisTextArea.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);
        popisTextArea.setMargin(new java.awt.Insets(3, 3, 3, 3));
        popisScrollPane.setViewportView(popisTextArea);

        vlastnikLabel.setText("Vlastník:");

        ArrayList <Uzivatel> uzivatele = null;

        if(uzivatel.getRank() == 0 && (this.ukol == null ||
            (this.ukol != null && this.ukol.getModel().getVlastnik() == null))) {
        uzivatele = new ArrayList<Uzivatel>();
        uzivatele.add(uzivatel);
    } else {
        try{
            uzivatele = Launcher.getInstance().getDatabase().vyberUzivatele("");
        }catch(DatabazovaChyba ex) {
            JOptionPane.showMessageDialog(this.getFocusOwner(), "Chyba v komunikaci s databází\n"+ex.getMessage());
        }
    }

    DefaultComboBoxModel vlastnikModel = new DefaultComboBoxModel();
    vlastnikModel.addElement(null);
    for(Uzivatel u : uzivatele) {
        vlastnikModel.addElement(u);
    }
    vlastnikComboBox.setModel(vlastnikModel/*new javax.swing.DefaultComboBoxModel(moznosti//new String[] { "Jan Novák", "Josef Velký" })*/);
    vlastnikComboBox.setRenderer(new VlastnikComboBoxRenderer());
    if(this.ukol == null || this.ukol.getModel().getVlastnik() == null) {
        vlastnikComboBox.setSelectedItem(null);
    } else {
        vlastnikComboBox.setSelectedItem(this.ukol.getModel().getVlastnik());
    }

    if(uzivatel.getRank() == 0) {
        if((this.ukol == null || (this.ukol != null && this.ukol.getModel().getVlastnik() == null)))
        vlastnikComboBox.setEnabled(true);
        else
        vlastnikComboBox.setEnabled(false);
    }

    komentarLabel.setText("Komentáře:");

    komentarEditorPane.setContentType("text/html");
    komentarEditorPane.setEditable(false);
    displayComments();
    komentarScrollPane.setViewportView(komentarEditorPane);

    dateCreateTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
    dateCreateTextField.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);
    dateCreateTextField.setDoubleBuffered(true);
    dateCreateTextField.setEnabled(false);
    if(this.ukol == null) {
        GregorianCalendar time = new GregorianCalendar();
        time.setTime(new Date(System.currentTimeMillis()));
        dateCreateTextField.setText(time.get(Calendar.DAY_OF_MONTH)+"." + (int)(time.get(Calendar.MONTH)+1)
            +"." + time.get(Calendar.YEAR));
    } else {
        GregorianCalendar time = this.ukol.getModel().getCasStart();
        dateCreateTextField.setText(time.get(Calendar.DAY_OF_MONTH)+"." + (int)(time.get(Calendar.MONTH)+1)
            +"." + time.get(Calendar.YEAR));
    }

    dateFinishTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
    dateFinishTextField.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);
    dateFinishTextField.setEnabled(false);
    if(this.ukol == null || this.ukol.getModel().getCasKonec() == null) {
        dateFinishTextField.setText("");
    } else {
        GregorianCalendar time = this.ukol.getModel().getCasKonec();
        dateFinishTextField.setText(time.get(Calendar.DAY_OF_MONTH) + "." + (int)(time.get(Calendar.MONTH)+1)
            + "." + time.get(Calendar.YEAR));
    }

    dateCreateLabel.setText("Vytvoření:");

    dateFinishLabel.setText("Ukončení:");

    dateDeadTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
    dateDeadTextField.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);
    if(this.ukol == null || this.ukol.getModel().getCasDead() == null) {
        dateDeadTextField.setText("");
    } else {
        GregorianCalendar time = this.ukol.getModel().getCasDead();
        dateDeadTextField.setText(time.get(Calendar.DAY_OF_MONTH) + "." + (int)(time.get(Calendar.MONTH)+1)
            + "." + time.get(Calendar.YEAR));
    }

    if(uzivatel.getRank() == 0)
    dateDeadTextField.setEnabled(false);

    dateDeadLabel.setText("Deadline:");

    saveButton.setText("Uložit");
    if(uzivatel.getRank() == 0)
    if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
    saveButton.setEnabled(false);
    saveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveButtonActionPerformed(evt);
        }
    });

    deleteButton.setText("Smazat");
    if(uzivatel.getRank() == 0 || this.ukol == null)
    deleteButton.setEnabled(false);
    deleteButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            deleteButtonActionPerformed(evt);
        }
    });

    stavLabel.setText("Stav:");

    stavComboBox.setModel(new javax.swing.DefaultComboBoxModel(UkolStatus./*stavy/*/values()));
    stavComboBox.setRenderer(new StavComboBoxRenderer());
    if(this.ukol == null || this.ukol.getModel().getStatus() == null) {
        stavComboBox.setSelectedItem(UkolStatus.NEW);
    } else {
        stavComboBox.setSelectedItem(this.ukol.getModel().getStatus());
    }

    if(uzivatel.getRank() == 0) {
        if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
        stavComboBox.setEnabled(false);
        else
        stavComboBox.removeItem(UkolStatus.CONFIRMED);
    }

    cancelButton.setText("Zrušit");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cancelButtonActionPerformed(evt);
        }
    });

    projektLabel.setText("Projekt:");

    ArrayList<ProjektControl> projekty = null;

    try {
        projekty = Launcher.getInstance().getDatabase().vyberProjekty("");
    } catch(DatabazovaChyba ex) {
        JOptionPane.showMessageDialog(this.getFocusOwner(), "Chyba v komunikaci s databází\n"+ex.getMessage());
    }

    DefaultComboBoxModel projektModel = new DefaultComboBoxModel();
    //listModel.addElement(null);
    for(ProjektControl p : projekty) {
        projektModel.addElement(p);
    }
    projektComboBox.setModel(projektModel/*new javax.swing.DefaultComboBoxModel(/*projektMoznosti )*/);
    projektComboBox.setRenderer(new ProjektComboBoxRenderer());
    if(this.ukol == null || this.ukol.getModel().getProjekt() == null) {
        projektComboBox.setSelectedIndex(0);
    } else {
        projektComboBox.setSelectedItem(this.ukol.getModel().getProjekt());
    }

    if(uzivatel.getRank() == 0)
    if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
    projektComboBox.setEnabled(false);

    casStravenLabel.setText("Čas stráven:");

    if(this.ukol == null)
    casStravenTextField.setText("0");
    else
    casStravenTextField.setText(Integer.toString(this.ukol.getModel().getCasStraven()));

    if(uzivatel.getRank() == 0)
    if(this.ukol != null && this.ukol.getModel().getVlastnik() != uzivatel)
    casStravenTextField.setEnabled(false);
    casStravenTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
    casStravenTextField.setDisabledTextColor(GUIConstants.DISABLED_TEXT_COLOR);

    komentarEntryTextArea.setColumns(20);
    komentarEntryTextArea.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    komentarEntryTextArea.setLineWrap(true);
    komentarEntryTextArea.setRows(5);
    komentarEntryTextArea.setWrapStyleWord(true);
    komentarEntryTextArea.setMargin(new java.awt.Insets(3, 3, 3, 3));
    komentarEntryScrollPane.setViewportView(komentarEntryTextArea);

    komentarAddButton.setText("Přidat komentář");
    komentarAddButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            komentarAddButtonActionPerformed(evt);
        }
    });

    ukolMenu.setText("Úkol");
    ukolMenuBar.add(ukolMenu);

    setJMenuBar(ukolMenuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(popisLabel)
                .addComponent(dateCreateLabel)
                .addComponent(nazevLabel)
                .addComponent(komentarLabel)
                .addComponent(vlastnikLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(popisScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(nazevTextField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(dateCreateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dateFinishLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dateFinishTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dateDeadLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dateDeadTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(casStravenLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(casStravenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(saveButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(deleteButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cancelButton))
                        .addComponent(komentarScrollPane, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(vlastnikComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(stavLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(stavComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(projektLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(projektComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(komentarEntryScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
                        .addComponent(komentarAddButton, javax.swing.GroupLayout.Alignment.TRAILING))))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(nazevTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(nazevLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(dateCreateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dateFinishLabel)
                .addComponent(dateFinishTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dateDeadLabel)
                .addComponent(dateDeadTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dateCreateLabel)
                .addComponent(casStravenLabel)
                .addComponent(casStravenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(6, 6, 6)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(popisScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(popisLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(vlastnikLabel)
                .addComponent(vlastnikComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(stavLabel)
                .addComponent(stavComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(projektLabel)
                .addComponent(projektComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(9, 9, 9)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(komentarLabel)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(komentarEntryScrollPane, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(komentarScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                    .addGap(11, 11, 11)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(saveButton)
                        .addComponent(deleteButton)
                        .addComponent(cancelButton)
                        .addComponent(komentarAddButton))))
            .addContainerGap(20, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        boolean error = false;
//        System.out.println(popisTextArea.getText());

        GUIConstants.odznacChybu(dateDeadTextField);
        if (GUIConstants.checkDeadline(this.dateDeadTextField.getText())) {
            GUIConstants.ukazChybu(dateDeadTextField);
            error = true;
        }
        
        GUIConstants.odznacChybu(nazevTextField);
        if(nazevTextField.getText().isEmpty()) {
            GUIConstants.ukazChybu(nazevTextField);
            error = true;
        }

        GUIConstants.odznacChybu(casStravenTextField);
        if (GUIConstants.checkCasStraven(this.casStravenTextField.getText())) {
            GUIConstants.ukazChybu(casStravenTextField);
            error = true;
        }

        if (error) {
            return;
        }

        if (this.ukol == null) {
            try {
                UkolControl.createUkol(-1, nazevTextField.getText(), popisTextArea.getText(), GUIConstants.preprocDeadline(this.dateDeadTextField.getText()),
                        (ProjektControl) projektComboBox.getSelectedItem(), (Uzivatel) vlastnikComboBox.getSelectedItem(),
                        (UkolStatus) stavComboBox.getSelectedItem(), Integer.parseInt(casStravenTextField.getText()));
            } catch (DatabazovaChyba ex) {
                JOptionPane.showMessageDialog(this.getFocusOwner(), "Chyba v komunikaci s databází\n" + ex.getMessage());
            }
        } else {
            try {
                this.ukol.editUkol(nazevTextField.getText(), popisTextArea.getText(),
                        GUIConstants.preprocDeadline(this.dateDeadTextField.getText()), (ProjektControl) projektComboBox.getSelectedItem(),
                        (Uzivatel) vlastnikComboBox.getSelectedItem(), (UkolStatus) stavComboBox.getSelectedItem(), Integer.parseInt(casStravenTextField.getText()));
            } catch (DatabazovaChyba ex) {
                JOptionPane.showMessageDialog(this.getFocusOwner(), "Chyba v komunikaci s databází\n" + ex.getMessage());
            }
        }

        Launcher.getInstance().refresh();
        
        this.dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        
        int n = JOptionPane.showConfirmDialog(
                this,
                "Opravdu chcete smazat vybraný úkol?\n"
                + "Tuto akci nelze vrátit!",
                "Potvrzení",
                JOptionPane.YES_NO_OPTION);
        if (n == /*0 */JOptionPane.YES_OPTION) {
        
            try {
                this.ukol.deleteUkol();
            } catch (DatabazovaChyba ex) {
                JOptionPane.showMessageDialog(this, "Chyba v komunikaci s databází\n" + ex.getMessage());
            }
            Launcher.getInstance().refresh();
            JOptionPane.showMessageDialog(this, "Úkol smazán.");
            this.dispose();
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void komentarAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_komentarAddButtonActionPerformed
        try {
            this.ukol.addKomentar(this.komentarEntryTextArea.getText(), Launcher.getInstance().getUzivatel());
            this.komentarEntryTextArea.setText("");
            displayComments();
        } catch (DatabazovaChyba ex) {
            JOptionPane.showMessageDialog(this, "Chyba v komunikaci s databází\n" + ex.getMessage());
        }
    }//GEN-LAST:event_komentarAddButtonActionPerformed

    private void displayComments() {
        String text = "";
        for(Komentar k : this.ukol.getModel().getKomentare()) {
            GregorianCalendar time = k.getTimeCreation();
            
            text = text + "<b>Od:</b> ";
            
            if(k.getAuthor() == null)
                text = text + "neznamy" + "<br>";
            
            else
                text = text  + k.getAuthor().getJmeno() + " " + k.getAuthor().getPrijmeni() + " ("
                + k.getAuthor().getEmail() + ")<br>"; 
            
            text = text + "<b>Čas:</b> " + time.get(Calendar.DAY_OF_MONTH) + "." 
                    + (int)(time.get(Calendar.MONTH)+1) + "." + 
                    time.get(Calendar.YEAR) + "<br>" + "<b>Text:</b> " +
                    k.getCommentText() + "<br><hr>";
        }
        
        komentarEditorPane.setText(text);
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel casStravenLabel;
    private javax.swing.JTextField casStravenTextField;
    private javax.swing.JLabel dateCreateLabel;
    private javax.swing.JTextField dateCreateTextField;
    private javax.swing.JLabel dateDeadLabel;
    private javax.swing.JTextField dateDeadTextField;
    private javax.swing.JLabel dateFinishLabel;
    private javax.swing.JTextField dateFinishTextField;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton komentarAddButton;
    private javax.swing.JEditorPane komentarEditorPane;
    private javax.swing.JScrollPane komentarEntryScrollPane;
    private javax.swing.JTextArea komentarEntryTextArea;
    private javax.swing.JLabel komentarLabel;
    private javax.swing.JScrollPane komentarScrollPane;
    private javax.swing.JLabel nazevLabel;
    private javax.swing.JTextField nazevTextField;
    private javax.swing.JLabel popisLabel;
    private javax.swing.JScrollPane popisScrollPane;
    private javax.swing.JTextArea popisTextArea;
    private javax.swing.JComboBox projektComboBox;
    private javax.swing.JLabel projektLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox stavComboBox;
    private javax.swing.JLabel stavLabel;
    private javax.swing.JMenu ukolMenu;
    private javax.swing.JMenuBar ukolMenuBar;
    private javax.swing.JComboBox vlastnikComboBox;
    private javax.swing.JLabel vlastnikLabel;
    // End of variables declaration//GEN-END:variables
}
